/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * ContactList.java
 *
 * Created on 10/08/2011, 18:48:57
 */
package org.jebert.javamessage.view;

import java.net.InetAddress;
import java.util.HashMap;
import java.util.Map;
import javax.swing.DefaultListModel;
import org.jebert.javamessage.control.ContactDiscover;
import org.jebert.javamessage.control.ContactMulticast;
import org.jebert.javamessage.control.MessageListener;
import org.jebert.javamessage.control.Translator;
import org.jebert.javamessage.model.Contact;

/**
 *
 * @author jebert
 */
public class ContactList extends javax.swing.JFrame {

    private DefaultListModel dlm = new DefaultListModel();
    private String cName;
    private Translator translator;
    private Map<InetAddress, ChatWindow> chatWindows = new HashMap<InetAddress, ChatWindow>();

    /** Creates new form ContactList */
    public ContactList(Translator trans, String contactName) {
        this.translator = trans;
        this.cName = contactName;
        initComponents();
        jlContacList.setModel(dlm);
        ContactMulticast cm = new ContactMulticast(cName);
        cm.start();
        ContactDiscover cd = new ContactDiscover(dlm, translator);
        cd.start();
        MessageListener ml = new MessageListener(this);
        ml.start();
        
    }
    public ChatWindow getChatWindow(InetAddress hostAddress){
    return chatWindows.get(hostAddress);
    }
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jlContacList = new javax.swing.JList();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jPanel1.setName("ContactList"); // NOI18N

        jlContacList.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jlContacListMouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(jlContacList);

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 240, Short.MAX_VALUE)
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 347, Short.MAX_VALUE)
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jlContacListMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jlContacListMouseClicked

        Contact cnt = (Contact) jlContacList.getSelectedValue();
        System.out.println(cnt.getIdleTime());
        if (cnt != null) {
            ChatWindow cw = new ChatWindow(cnt, translator);
            chatWindows.put(cnt.getHost(), cw);
            cw.setVisible(true);
        } else {
            ChatWindow cw = chatWindows.get(cnt.getHost());
            cw.setVisible(true);            
            cw.setLog();            
            System.out.println(cnt.getIdleTime());
        }

    }//GEN-LAST:event_jlContacListMouseClicked
    /**
     * @param args the command line arguments
     */
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JList jlContacList;
    // End of variables declaration//GEN-END:variables
}
